package com.zkr.kfgs.service;

import com.zkr.kfgs.mapper.KFGSMapper;
import com.zkr.kfgs.model.*;
import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2021/1/19.
 */
@Service
public class KFGSServiceImpl implements KFGSService {

    @Autowired
    private KFGSMapper kfgsMapper;

  @Override
    public ResultVo queryPage(KFGSSelectVo kfgsSelectVo) {
        int count = kfgsMapper.queryCount(kfgsSelectVo);
        List<KFGSVo> kfgsVoList = kfgsMapper.queryPage(kfgsSelectVo);
        for (KFGSVo kfgsVo:kfgsVoList){
            Integer status = kfgsVo.getStatus() == true?1:0;
            for (StatusEnum c:StatusEnum.values()){
                if (c.getValue().equals(status)){
                    kfgsVo.setStatusName(c.getName());
                }
            }
        }
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(kfgsSelectVo.getPageIndex());
        allRecords.setDataList(kfgsVoList);
        allRecords.setPageSize(kfgsSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);

    }

    @Override
    public ResultVo queryKuWei(String kqcode) {
        List<KuWeiVo> kuWeiVoList = kfgsMapper.queryKuWei(kqcode);
        return ResultBuilderVo.success(kuWeiVoList);

    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid KFGSUpdateStatusVo kfgsUpdateStatusVo) {
        KFGSVo kfgsVo = new KFGSVo();
        BeanUtils.copyProperties(kfgsUpdateStatusVo,kfgsVo);
        kfgsVo.setUpdateName("李四");
        kfgsVo.setUpdateTime(new Date());
        kfgsMapper.updateStatus(kfgsVo);
        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo ofCurrentUser() {
        List<LegelPersonVo> legelPersonVoList = kfgsMapper.ofCurrentUser();
        return ResultBuilderVo.success(legelPersonVoList);

    }

    @Override
    public ResultVo queryWarehouse(KFGSVo kfgsVo) {
        List<KFGSVo> kfgsVoList = kfgsMapper.queryWarehouse(kfgsVo);
        return ResultBuilderVo.success(kfgsVoList);
    }

    @Override
    public ResultVo queryCompanyStorageById(Integer id) {
        List<KFGSVo> kfgsVoList = kfgsMapper.queryCompanyStorageById(id);
        return ResultBuilderVo.success(kfgsVoList);
    }



    @Override
    public ResultVo queryKuqu(String wareHouseCode) {
        /*KQVo kqVo = kfgsMapper.queryKuqu(wareHouseCode);*/
        List<KQVo> kqVoList = kfgsMapper.queryKuqu(wareHouseCode);
        return ResultBuilderVo.success(kqVoList);


    }

    @Override
    @Transactional
    public ResultVo addUpdate(@Valid List<KFGSAddVo> addLegal) {
        List<KFGSVo> kfgsVoList = kfgsMapper.queryPositionId(addLegal);
        for (KFGSVo vo : kfgsVoList) {
            if(vo==null){
                return ResultBuilderVo.error("库位id不存在");
            }
        }
        //把库位中所有该法人id设置为null
        kfgsMapper.updateKong(addLegal);
        //新增
        kfgsMapper.addUpdate(addLegal);
        return ResultBuilderVo.success();

    }



    @Override
    public ResultVo querykqsx() {
        List<EulogyVo> eulogyVoList = kfgsMapper.querykqsx();
        return ResultBuilderVo.success(eulogyVoList);

    }

    @Override
    public ResultVo queryWarehousess(@Valid KQVo kqVo) {
        //判断库房id是否存在
        KFVo kfVo = kfgsMapper.queryByHouseId(kqVo);
        if(kfVo==null){
            return ResultBuilderVo.error("库房id不存在");
        }
        //判断库区属性id是否存在
        List<KQVo> kqVos = kfgsMapper.queryByShuXing(kqVo);
        if (kqVos.size() != kqVo.getIds().size()){
            return ResultBuilderVo.error("库区属性id不存在");
        }
        KQVo kqVo1 = kfgsMapper.querySection(kqVo);
        return ResultBuilderVo.success(kqVo1);

    }

    @Override
    public ResultVo queryFangQuWei(Integer legalPersonId) {
        //查询法人已选库房
        List<Integer> warehouseIds = kfgsMapper.querywarehouseIds(legalPersonId);
        //查询法人已选库区
        List<Integer> kids = kfgsMapper.querykuqu(legalPersonId);
        //查询法人已选库位
        List<Integer> wids = kfgsMapper.querykuwei(legalPersonId);

        List<Object> affiliationList = new ArrayList<>();
        affiliationList.add(warehouseIds);
        affiliationList.add(kids);
        affiliationList.add(wids);
        return ResultBuilderVo.success(affiliationList);

    }


}
